#! /usr/bin/python3
# -*- coding: UTF-8 -*-

if __name__ == "__main__":
	raise Exception("不支持从这里调用")

__version__ = "1.0"
__all__ = []

from hashlib import md5
from uuid import uuid4
from decimal import Decimal
import pandas as pd

from com.mousechsh.business.lib.model.MouseChshAppModel import MouseChshAppModel
from com.mousechsh.business.lib.MouseChshAuthorizationAnnotition import MouseChshAuthorizationAnnotition
from com.mousechsh.business.lib.model.MouseChshDomainModel import MouseChshDomainModel
from com.mousechsh.business.lib.model.MouseChshKeyValueModel import MouseChshKeyValueModel
from com.mousechsh.business.lib.model.MouseChshLoginModel import MouseChshLoginModel
from com.mousechsh.business.lib.model.MouseChshModuleModel import MouseChshModuleModel
from com.mousechsh.business.lib.model.MouseChshUserModel import MouseChshUserModel
from com.mousechsh.common.data.MouseChshDao import MouseChshDao
from com.mousechsh.common.data.json.MouseChshJson import mousechsh_json_from_json, mousechsh_json_to_json
from com.mousechsh.common.io.cache.MouseChshRedisCache import MouseChshRedisCache
from com.mousechsh.common.io.db.MouseChshPostgresql import MouseChshPostgresql
from com.mousechsh.common.io.file.MouseChshFile import mousechsh_file_write_binary
from com.mousechsh.common.middle.auth.MouseChshJwt import mousechsh_jwt_encode_string
from com.mousechsh.common.middle.conf.MouseChshConfiguration import mousechsh_configuration_get
from com.mousechsh.common.middle.http.MouseChshHttpHeader import mousechsh_http_header_accept_language_annotation, \
	mousechsh_http_header_authorization_annotation
from com.mousechsh.common.middle.http.MouseChshHttpRequest import MouseChshHttpRequest
from com.mousechsh.common.middle.http.MouseChshHttpMultiPartBody import MouseChshHttpMultiPartBody
from com.mousechsh.common.middle.rest_api.MouseChshRestApi import mousechsh_rest_api_annotation, mousechsh_rest_pager_api_annotation
from com.mousechsh.common.middle.router.MouseChshCorsAnnotition import mousechsh_cors_annotation
from com.mousechsh.common.middle.router.MouseChshHttpRouter import mousechsh_http_router_annotation
from com.mousechsh.common.util.MouseChshDateTime import MouseChshDateTime
from com.mousechsh.common.log.MouseChshLog import mousechsh_logging, mousechsh_logging_data, mousechsh_logging_exception
from com.mousechsh.common.util.MouseChshMath import mousechsh_math_gen_uuid


@mousechsh_http_router_annotation(method=['GET', 'POST', 'OPTIONS'], path='/get/ne/map')
@mousechsh_cors_annotation()
@mousechsh_http_header_accept_language_annotation()
@mousechsh_rest_pager_api_annotation()
def GetNetworkElementForMap(params):

	pageSize = params['pageSize'] if 'pageSize' in params else 10
	pageIndex = ( params['pageIndex'] if 'pageIndex' in params else 1 )
	bound = ( params['bound'] if 'bound' in params else '((0,0),(0,0))' )

	db = MouseChshPostgresql()
	db.connect()
	count = db.get(
		'''
SELECT
	COUNT(*) AS "Count"
FROM "A_WorkTickets"."T_NetworkElement"
WHERE "C_Type" = '局站' AND "C_UseStatus" = '在用'
AND "C_Location" <@ box %(bound)s
		''', {
			'bound': bound,
		}
	)
	data = db.get(
		'''
SELECT
	"C_Id" AS "Id",
	"C_Name" AS "Name",
	"C_Code" AS "Code",
	"C_Location" AS "Location"
FROM "A_WorkTickets"."T_NetworkElement"
WHERE "C_Type" = '局站' AND "C_UseStatus" = '在用'
AND "C_Location" <@ box %(bound)s
ORDER BY "C_Code" ASC
LIMIT %(pageSize)s OFFSET %(pageIndex)s
''', {
			'bound': bound,
			'pageSize': pageSize,
			'pageIndex': ( ( pageIndex - 1 ) * pageSize if pageIndex - 1 > 0 else 0 )
		}
	)
	db.close()

	result = []
	for item in data['data']:
		obj = {}
		i = 0
		for col in data['meta']:
			obj[col.name] = item[i]
			i += 1
		result.append(obj)

	return {
		'data': result,
		'pageIndex': pageIndex,
		'pageSize': pageSize,
		'totalData': count['data'][0][0]
	}

@mousechsh_http_router_annotation(method=['GET', 'POST', 'OPTIONS'], path='/get/ne/map/search')
@mousechsh_cors_annotation()
@mousechsh_http_header_accept_language_annotation()
@mousechsh_rest_pager_api_annotation()
def SearchNetworkElementForMap(params):

	pageSize = params['pageSize'] if 'pageSize' in params else 10
	pageIndex = ( params['pageIndex'] if 'pageIndex' in params else 1 )
	name = ( params['name'] if 'name' in params else '' )

	db = MouseChshPostgresql()
	db.connect()
	count = db.get(
		'''
SELECT
	COUNT(*) AS "Count"
FROM "A_WorkTickets"."T_NetworkElement"
WHERE "C_Type" = '局站' AND "C_UseStatus" = '在用'
AND ( "C_Name" ILIKE %(name)s OR "C_Code" ILIKE %(name)s )
		''', {
			'name': '%' + name + '%',
		}
	)
	data = db.get(
		'''
SELECT
	"C_Id" AS "Id",
	"C_Name" AS "Name",
	"C_Code" AS "Code",
	"C_Location" AS "Location"
FROM "A_WorkTickets"."T_NetworkElement"
WHERE "C_Type" = '局站' AND "C_UseStatus" = '在用'
AND ( "C_Name" ILIKE %(name)s OR "C_Code" ILIKE %(name)s )
ORDER BY "C_Code" ASC
LIMIT %(pageSize)s OFFSET %(pageIndex)s
''', {
			'name': '%' + name + '%',
			'pageSize': pageSize,
			'pageIndex': ( ( pageIndex - 1 ) * pageSize if pageIndex - 1 > 0 else 0 )
		}
	)
	db.close()

	result = []
	for item in data['data']:
		obj = {}
		i = 0
		for col in data['meta']:
			obj[col.name] = item[i]
			i += 1
		result.append(obj)

	return {
		'data': result,
		'pageIndex': pageIndex,
		'pageSize': pageSize,
		'totalData': count['data'][0][0]
	}

@mousechsh_http_router_annotation(method=['GET', 'POST', 'OPTIONS'], path='/get/ne/map/detail')
@mousechsh_cors_annotation()
@mousechsh_http_header_accept_language_annotation()
@mousechsh_rest_api_annotation()
def GetNetworkElementForMapDetail(params):

	element_id = params['ElementId'] if params['ElementId'] else ''

	db = MouseChshPostgresql()
	db.connect()
	data = db.get(
		'''
SELECT
	"C_Id" AS "Id",
	"C_Location" AS "Location",
	"C_Name" AS "Name",
	"C_Code" AS "Code",
	"C_Type" AS "Type",
	"C_UseStatus" AS "UseStatus",
	"C_Zone" AS "Zone",
	"C_GovZone" AS "GovZone",
	"C_Address" AS "Address",
	"C_InGridName" AS "InGridName",
	"C_StationType" AS "StationType",
	"C_StationProp" AS "StationProp",
	"C_NetworkLevel" AS "NetworkLevel",
	"C_InCityRing" AS "InCityRing",
	"C_TietaCode" AS "TietaCode",
	"C_WorkGridUserName" AS "WorkGridUserName"
FROM "A_WorkTickets"."T_NetworkElement"
WHERE "C_Id" = %(id)s
LIMIT 1
''', {
			'id': element_id
		}
	)
	db.close()

	result = {}
	for item in data['data']:
		i = 0
		for col in data['meta']:
			result[col.name] = item[i]
			i += 1

	return result
